define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'material_purchase_request/index' + location.search,
                    add_url: 'material_purchase_request/add',
                    edit_url: 'material_purchase_request/edit',
                    del_url: 'material_purchase_request/del',
                    multi_url: 'material_purchase_request/multi',
                    import_url: 'material_purchase_request/import',
                    table: 'material_purchase_request',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'request_name', title: __('Request_name'), operate: 'LIKE'},
                        {field: 'material_type', title: __('Material_type'), searchList: {"原料":__('原料'),"包材":__('包材')}, formatter: Table.api.formatter.normal},
                        {field: 'supplier_id', title: __('Supplier_id')},
                        {field: 'material_id', title: __('Material_id')},
                        {field: 'quantity', title: __('Quantity'), operate:'BETWEEN'},
                        {field: 'unit_price', title: __('Unit_price'), operate:'BETWEEN'},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-supplier_id").data("params", function (obj) {
                    return {type: $('#c-material_type').val() == '原料' ? 0 : 1};
            });
            $("#c-unit_price").data("params", function (obj) {
                    return {'supplier_id': getSupplierId(), 'material_id': getMaterialId()};
            });
            $("#c-supplier_id").on('change', function() {
                $('#c-material_id').selectPageRefresh();;
                $('#c-material_id').selectPageClear();
            });
            // 监听goods_type变化
            $('#c-material_type').on('change', function() {
                $('#c-supplier_id').selectPageClear();                
            });
            // 使用goods_id携带searchValue参数
            $('#c-material_id').data('params', function (obj) {
                return {searchValue: $('#c-supplier_id').val(), searchKey: 'id'};
            })

            // 监听数量和价格变化，自动计算金额
            $('#c-quantity, #c-unit_price').on('change', function() {
                var quantity = parseFloat($('#c-quantity').val()) || 0;
                var price = parseFloat($('#c-unit_price').val()) || 0;
                $('#c-total_amount').val((quantity * price).toFixed(2));
            });

            $("#c-supplier_id").data("eSelect", function(data){
                $("#c-Supplier_id").val(data.id);
            });
            $("#c-material_id").data("eSelect", function(data){
                $("#c-Material_id").val(data.id);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-supplier_id").data("params", function (obj) {
                    return {type: $('#c-material_type').val() == '原料' ? 0 : 1};
            });
            $("#c-unit_price").data("params", function (obj) {
                    return {'supplier_id': getSupplierId(), 'material_id': getMaterialId()};
            });
            $("#c-supplier_id").on('change', function() {
                $('#c-material_id').selectPageRefresh();;
                $('#c-material_id').selectPageClear();
            });
            // 监听goods_type变化
            $('#c-material_type').on('change', function() {
                $('#c-supplier_id').selectPageClear();                
            });
            // 使用goods_id携带searchValue参数
            $('#c-material_id').data('params', function (obj) {
                return {searchValue: $('#c-supplier_id').val(), searchKey: 'id'};
            })

            // 监听数量和价格变化，自动计算金额
            $('#c-quantity, #c-unit_price').on('change', function() {
                var quantity = parseFloat($('#c-quantity').val()) || 0;
                var price = parseFloat($('#c-unit_price').val()) || 0;
                $('#c-total_amount').val((quantity * price).toFixed(2));
            });

            $("#c-supplier_id").data("eSelect", function(data){
                $("#c-Supplier_id").val(data.id);
            });
            $("#c-material_id").data("eSelect", function(data){
                $("#c-Material_id").val(data.id);
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    function getSupplierId() {
        return $('#c-Supplier_id').val();
    }
    function getMaterialId() {
        return $('#c-Material_id').val();
    }
    return Controller;
});
